package com.thunder.kocho.loki;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Rect;
import com.thunder.kocho.loki.builder.KeyFrameAnimatorBuilder;
import com.thunder.kocho.loki.shape.DiamondLoki;
import java.util.ArrayList;

/**
 * @author Hilex
 * @date 2021-3-16
 */
public class RectJumper extends LokiGroup {


    @Override
    protected ArrayList<Loki> onCreateChild() {
        ArrayList<Loki> lokis=new ArrayList<>();
        for(int i=0;i<4;i++){
            Loki loki=new RectJumperLoki(i);
            loki.setAnimationDelays(0);
            lokis.add(loki);
        }
        return lokis;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        bounds=clipSquare(bounds);
        int cx=bounds.centerX();
        int cy= bounds.centerY();
        float side=bounds.width()/4f;
        for(int i=0;i<4;i++){
            Loki loki=mLokis.get(i);
            switch(i){
                case 0:
                    loki.setLokiBoundsAndPivot(cx-side,cy-side/2f,cx,cy+side/2f);
                    break;
                case 1:
                    loki.setLokiBoundsAndPivot(cx-side/2f,cy-side,cx+side/2f,cy);
                    break;
                case 2:
                    loki.setLokiBoundsAndPivot(cx,cy-side/2f,cx+side,cy+side/2f);
                    break;
                case 3:
                    loki.setLokiBoundsAndPivot(cx-side/2f,cy,cx+side/2f,cy+side);
                    break;
            }
            //loki.setPivotX(cx);
            //loki.setPivotY(cy);
            if(loki instanceof DiamondLoki){
                ((DiamondLoki) loki).initPath();
            }
        }
    }

    @Override
    protected void onChildCreated(ArrayList<Loki> lokis) {

    }

    private static class RectJumperLoki extends DiamondLoki{

        private final int index;

        public RectJumperLoki(int index){
            this.index=index;
        }

        @Override
        protected Animator onCreateAnimator() {
            return new KeyFrameAnimatorBuilder()
                    .rotate(new float[]{0,0.25f,0.5f,0.75f,1f},0,0,90,180,180)
                    .custom("TranslatePercentage",new float[]{0,0.1f,0.25f,0.75f,0.9f,1f},0f,0f,0.45f,0.45f,0f,0f)
                    .count(ValueAnimator.INFINITE)
                    .mode(ValueAnimator.REVERSE)
                    .duration(2000)
                    .target(this)
                    .build();
        }

        private void setTranslatePercentage(float percentage){
            switch (index){
                case 0:
                    setTranslatePercentageX(-percentage);
                    break;
                case 1:
                    setTranslatePercentageY(-percentage);
                    break;
                case 2:
                    setTranslatePercentageX(percentage);
                    break;
                case 3:
                    setTranslatePercentageY(percentage);
                    break;
            }
        }


    }


}
